package com.kevin.ymqquan.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.kevin.ymqquan.R;
import com.kevin.ymqquan.activity.CourseActivity.MyClubAsynTask;
import com.kevin.ymqquan.application.App;
import com.kevin.ymqquan.constants.Constants;
import com.kevin.ymqquan.custom.CustomToast;
import com.kevin.ymqquan.httppost.Helper;
import com.kevin.ymqquan.httppost.HttpPostService;

public class InfoActivity extends Activity {

	private App application;
	private TextView tv_club_detail_wechat_right;
	private TextView tv_club_detail_name_right;
	private TextView tv_club_detail_sname_right;
	private TextView tv_club_detail_sex_right;
	private TextView tv_club_detail_cid_right;
	private TextView tv_club_detail_birthday_right;
	private TextView tv_club_detail_phone_right;
	private TextView tv_club_detail_qq_right;
	private ImageView iv_clubapplyreason_back;

	private Dialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_info);

		init();
		
		if (Helper.checkConnection(InfoActivity.this)) {
			dialog = CustomToast.createLoadingDialog(InfoActivity.this,
					"数据获取中...");
			dialog.setCancelable(true);
			dialog.show();
			InfoAsyTask policyTask = new InfoAsyTask();
			policyTask.execute();

		} else {
			CustomToast.CustomActivityToast(InfoActivity.this,
					"网络连接异常，请检查您的网络", -80);
		}
	}

	private void init() {
		// TODO Auto-generated method stub
		application = (App) getApplication();
		iv_clubapplyreason_back = (ImageView) findViewById(R.id.iv_clubapplyreason_back);
		tv_club_detail_wechat_right = (TextView) findViewById(R.id.tv_club_detail_wechat_right);
		tv_club_detail_name_right = (TextView) findViewById(R.id.tv_club_detail_name_right);
		tv_club_detail_sname_right = (TextView) findViewById(R.id.tv_club_detail_sname_right);
		tv_club_detail_sex_right = (TextView) findViewById(R.id.tv_club_detail_sex_right);
		tv_club_detail_cid_right = (TextView) findViewById(R.id.tv_club_detail_cid_right);
		tv_club_detail_birthday_right = (TextView) findViewById(R.id.tv_club_detail_birthday_right);
		tv_club_detail_phone_right = (TextView) findViewById(R.id.tv_club_detail_phone_right);
		tv_club_detail_qq_right = (TextView) findViewById(R.id.tv_club_detail_qq_right);

		iv_clubapplyreason_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				InfoActivity.this.finish();
			}
		});
	}

	class InfoAsyTask extends AsyncTask<String, Object, String> {

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub

			String param = "{\"method\":\"getUserInfo\",\"userId\":\""
					+ application.userId + "\"}";
			String replyList = "";
			try {
				replyList = HttpPostService.PostJsonString(
						Constants.ServerAddr, param);
			} catch (Exception e) {
				e.printStackTrace();
			}
			// Log.i("kevinlog", param);
			// Log.i("kevinlog", replyList);
			return replyList;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			Log.i("kevinlog", result);
			JSONObject jsonObject;
			try {
				jsonObject = new JSONObject(result);
				String resultType = jsonObject.getString("status");
				if ("100".equals(resultType)) {
					CustomToast.CustomActivityToast(InfoActivity.this,
							"服务器异常，请稍后重试", 0);
					InfoActivity.this.finish();
				} else {
					tv_club_detail_wechat_right.setText("null".equals(jsonObject.getString("wechat"))?"":jsonObject.getString("wechat"));
					tv_club_detail_qq_right.setText("null".equals(jsonObject.getString("qq"))?"":jsonObject.getString("qq"));
					tv_club_detail_name_right.setText("null".equals(jsonObject.getString("name"))?"":jsonObject.getString("name"));
					tv_club_detail_sname_right.setText("null".equals(jsonObject.getString("sname"))?"":jsonObject.getString("sname"));
					tv_club_detail_cid_right.setText("null".equals(jsonObject.getString("cid"))?"":jsonObject.getString("cid"));
					tv_club_detail_sex_right.setText("null".equals(jsonObject.getString("sex"))?"":jsonObject.getString("sex"));
					tv_club_detail_birthday_right.setText("null".equals(jsonObject.getString("birthday"))?"":jsonObject.getString("birthday"));
					tv_club_detail_phone_right.setText("null".equals(jsonObject.getString("phone"))?"":jsonObject.getString("phone"));
				}

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				Log.i("kevinlog", "error");
				e.printStackTrace();
			}

			dialog.dismiss();
		}

	}
}
